  #
# Copyright 2013 - Tom Alessi
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


"""This module contains all of the views for the Berper main
   application relating to reporting functions.  

"""


from django.shortcuts import render_to_response
from django.template import RequestContext
from django.db.models import Count
from berper.main.models import Report
from berper.main.models import Not_Found_List
from berper.main.forms import URLListForm


def report(request):
    """Display an individual full report
    
    """

    # Check the form elements
    form = URLListForm(request.GET)

    if form.is_valid():
        id = form.cleaned_data['id']
        report_detail = Report.objects.filter(id=id).values('path','report_uid')

        # Print the page
        return render_to_response(
           'reports/report.html',
           {
              'title':'Berper | Full Report',
              'report_detail':report_detail
           },
           context_instance=RequestContext(request)
        )

    # Invalid Form
    else:
        print 'Invalid form: URLListForm: %s' % form.errors

        # Send them back to the home page
        return HttpResponseRedirect('/')


def sscans(request):
    """Spidered Scans Report page
    
    """

    reports = Report.objects.filter(name__scan_type__targeted=False).values(
                                                                      'id',
                                                                      'name__name',
                                                                      'name__site__site',
                                                                      'path',
                                                                      'start',
                                                                      'finish',
                                                                      'urls_scanned__urls',
                                                                      'requests_made__requests'
                                                                      ).annotate(not_found=Count('not_found_list__path')).order_by('start')

    # Print the page
    return render_to_response(
       'reports/sscans.html',
       {
          'title':'Berper | Spidered Scan Reports',
          'reports':reports
       },
       context_instance=RequestContext(request)
    )


def tscans(request):
    """Targeted Scans Report page
    
    """

    reports = Report.objects.filter(name__scan_type__targeted=True).values(
                                                                      'id',
                                                                      'name__name',
                                                                      'name__site__site',
                                                                      'path',
                                                                      'start',
                                                                      'finish',
                                                                      'urls_scanned__urls',
                                                                      'requests_made__requests'
                                                                      ).annotate(not_found=Count('not_found_list__path')).order_by('start')

    # Print the page
    return render_to_response(
       'reports/tscans.html',
       {
          'title':'Berper | Targeted Scan Reports',
          'reports':reports
       },
       context_instance=RequestContext(request)
    )


def not_found(request):
    """Listing of not found (404) urls
    
    """

    # Check the form elements
    form = URLListForm(request.GET)

    if form.is_valid():
        id = form.cleaned_data['id']
        list = Not_Found_List.objects.filter(report_id=id).values('path_id__path')

        # Print the page
        return render_to_response(
           'reports/not_found.html',
           {
              'title':'Berper | Not Found List',
              'list':list
           },
           context_instance=RequestContext(request)
        )

    # Invalid Form
    else:
        print 'Invalid form: URLListForm: %s' % form.errors

        # Send them back to the reports page
        return HttpResponseRedirect('/')

